import type { ResponseData } from '@/utils/types.d';
import { SUCCESS_CODE } from '@/utils/types.d';
import request from '@/utils/request';
import type { IWechatTemplateMsg } from './type';

export async function associateOneWechatTemplateMsg(data: IWechatTemplateMsg) {
  const res = await request(`/api/wechat-template-message/v1.0.0/template`, {
    method: 'PUT',
    data,
  });
  if (res?.code === SUCCESS_CODE) return { success: true, message: '配置成功', data: res.data };
  return { success: false, message: res?.message || '配置失败', data: res?.data };
}

export async function sendWechatTemplateMsg(data: {
  plateNumber: string;
  type: string;
  url?: string;
  content: Record<string, string>;
}) {
  const res = await request(`/api/wechat-template-message/v1.0.0/message`, {
    method: 'POST',
    data,
  });
  if (res?.code === SUCCESS_CODE) return { success: true, message: '发送成功', data: res.data };
  return { success: false, message: res?.message || '发送失败', data: res?.data };
}

export async function getWechatTemplateMsgList() {
  const res: ResponseData<IWechatTemplateMsg[]> = await request(
    `/api/wechat-template-message/v1.0.0/list`,
    {
      method: 'GET',
    },
  );
  if (res?.code === SUCCESS_CODE) return { success: true, message: '获取成功', data: res.data };
  return { success: false, message: res?.message || '获取失败', data: res?.data };
}

export async function changeTemplateState(template_id: string, checked: boolean) {
  if (!template_id) {
    return { success: false, message: '模板ID不能为空', data: null };
  }
  const res = await request(`/api/wechat-template-message/v1.0.0/${template_id}`, {
    method: 'PUT',
    data: { checked },
  });
  if (res?.code === SUCCESS_CODE) {
    return { success: true, message: '配置成功', data: res.data };
  } else {
    return { success: false, message: res?.message || '配置失败', data: res?.data };
  }
}
